package yutProject_OOP;

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class Screen extends Frame {

	private final String imageArray[][] = { { "title", "images/title.gif" }, { "yutpan", "images/yutpan.gif" } };

	private final int TITLE_INDEX = 0;
	private final int FILENAME_INDEX = 1;

	private final int SCREEN_WIDTH = 800;
	private final int SCREEN_HEIGHT = 600;

	private Mode mode;

	private MouseEventProcessor mouseEventProcessor = new MouseEventProcessor();
	private WindowAdapter closeButtonListener = new WindowAdapter() {
		public void windowClosing(WindowEvent e) {
			System.exit(1);
		}
	};

	private Map<String, Image> imageMap;

	Screen() {
		mode = Mode.Title;

		imageMap = new HashMap<String, Image>();
		setTitle("!!! 윷놀이 !!!");
		setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
		setCenterPosition();
		setResizable(false);

		setCloseButtonEnable(true);
		setMouseEventEnable(true);

		Buttons.init();
		
		loadImages();
	}

	public void on() {
		setVisible(true);
	}

	public void off() {
		setVisible(false);
	}

	private void setCloseButtonEnable(boolean value) {
		if (value) {
			addWindowListener(closeButtonListener);
		} else {
			removeWindowListener(closeButtonListener);
		}
	}

	private void setMouseEventEnable(boolean value) {
		if (value) {
			addMouseListener(mouseEventProcessor);
		} else {
			removeMouseListener(mouseEventProcessor);
		}
	}

	/**
	 * 스크린 출력 메소드. 화면 모드에 따라 달리 출력한다.
	 */
	@Override
	public void paint(Graphics g) {
	    switch (mode) {
		case Title:
			System.out.println("MODE : TITLE");
            paintTitle(g);
			break;
		case Game:
			System.out.println("MODE : GAME");
			g.drawImage(imageMap.get("yutpan"), 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, this);
		case EndTitle:
			g.drawImage(imageMap.get("endTitle"), 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, this);
		}
		super.paint(g);
	}

    private void paintTitle(Graphics g) {
        g.drawImage(imageMap.get("title"), 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, this);
        add(Buttons.playGameButton);
    }

	/**
	 * Frame 을 화면 중앙에 위치시킨다.
	 */
	private void setCenterPosition() {
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		int fullScreenWidth = ge.getMaximumWindowBounds().width;
		int fullScreenHeight = ge.getMaximumWindowBounds().height;

		setLocation(fullScreenWidth / 2 - SCREEN_WIDTH / 2, fullScreenHeight / 2 - SCREEN_HEIGHT / 2);
	}

	/**
	 * imageArray[][] 에 세팅된 이미지 로딩.
	 * 
	 * @return
	 */
	private boolean loadImages() {
		for (int i = 0; i < imageArray.length; i++) {
			File imageFile = new File(imageArray[i][FILENAME_INDEX]);
			if (imageFile.exists()) {
				imageMap.put(imageArray[i][TITLE_INDEX], getImage(imageArray[i][FILENAME_INDEX]));
			} else {
				System.out.println(String.format("%s not exists.", imageArray[i][FILENAME_INDEX]));
			}
		}
		return true;
	}

	/**
	 * 이미지를 가지고 온다. Toolkit.getDefaultToolkit().getImage() 를 보다 편하게 사용하기 위한
	 * 포장메소드.
	 * 
	 * @param path
	 * @return
	 */
	private Image getImage(String path) {
		return Toolkit.getDefaultToolkit().getImage(path);
	}

	/**
	 * 화면 출력 모드를 변경한다.
	 * 
	 * @param mode
	 */
	public void changeMode(Mode mode) {
		this.mode = mode;
		repaint();
	}
}
